
import xlwt


class SVMReport(object):
    
    def __init__(self,**kwargs):
        #Process only one mainset at a time
        #Note. one mainset spawns 19 datasets
        self.output_file    = ''
        #self.dset  -> {'gutmtpslhpcr_Colon_Lumen__Crohn_Colon__Crohn_Lumen': RESULTS isstance, ..}
        self.dset           = {}
        #self.dsort -> List of datasetname -> ['gutmtpslhpcr_Colon_Lumen__Crohn_Colon__Crohn_Lumen', ..] 
        self.dsort          = []
        self.nameabr        = []
        self.__dict__.update(kwargs)
        
    def xcelRecord(self):
        #Excel properties
        self.book   = xlwt.Workbook()
        col_width   = 6000      
        row_list    = ('Positive'   ,'Negative'     ,'POS/NEG_ORGN'     ,'POS/NEG_W'            ,'Scaling'      ,'Weight'   ,'1st SFS'      ,'1st NFS'  ,
                       'SVM Kernel' ,'2nd SFS'      ,'2nd NFS'          ,'Accuracy(%)'          ,'SVM (C,gamma)','AUC'      ,'TP'           ,'FP'       ,
                       'FN'         ,'TN'           ,'Sens'             ,'Spec'                 ,'PPV'          ,'NPV'      ,'F-Measure'    ,)                        
        self.sheet  = {}
        #self.namesort -> ['gutmtpslhpcr_Entire','gutmtpslhpcr_Colon_Lumen',..]
        for i in range(len(self.dsort)):
            dname           = self.dsort[i]
            #subname -> 'Crohn_Colon__Crohn_Lumen'
            nameAbr         = self.nameabr[i]
            #result_obj -> RESULTS instance
            result_obj      = self.dset[dname]
            results_svm     = result_obj.results_svm
            results         = results_svm.results # List of UniverseDATA instances
            if len(results) == 0:
                continue 
            cur_col                             = 0
            self.sheet[i]                       = self.book.add_sheet(nameAbr)
            self.sheet[i].col(cur_col).width    = col_width
            self.sheet[i].write(0,cur_col,dname)
            for j in range(len(row_list)):
                self.sheet[i].write(j+1,cur_col,row_list[j])
            for j in results:
                #j -> UniverseDATA instance
                best                                = j
                mainname                            = best.datasetname
                #subname -> 'Crohn_Colon__Crohn_Lumen'
                subname                             = '__'.join(mainname.split('__')[1:]) 
                cur_col                             += 1
                self.sheet[i].col(cur_col).width    = col_width
                pn                                  = subname.split('__')
                clsnum                              = best.ttcls.split('@')[1]
                if 'Healthy Control' in pn:
                    haveHealthy                         = True
                    Positive                            = pn[0] if pn[0] != 'Healthy Control' else pn[1]
                    Negative                            = pn[1] if pn[1] == 'Healthy Control' else pn[0]
                else:
                    haveHealthy                         = False
                    Positive                            = clsnum.split(';')[0].split(':')[0]
                    Negative                            = clsnum.split(';')[1].split(':')[0]
                if haveHealthy:
                    pnum    = clsnum.split(';')[0].split(':')[1] if clsnum.split(';')[0].split(':')[0] != 'Healthy Control' else clsnum.split(';')[1].split(':')[1]
                    nnum    = clsnum.split(';')[0].split(':')[1] if clsnum.split(';')[0].split(':')[0] == 'Healthy Control' else clsnum.split(';')[1].split(':')[1]
                else:
                    pnum    = clsnum.split(';')[0].split(':')[1]
                    nnum    = clsnum.split(';')[1].split(':')[1]
                p_n     = '%s/%s' % (pnum,nnum)
                if best.ttcls_w != None:
                    clsnum_w    = best.ttcls_w.split('@')[1]
                    if haveHealthy:
                        pnum_w      = clsnum_w.split(';')[0].split(':')[1] if clsnum_w.split(';')[0].split(':')[0] != 'Healthy Control' else clsnum_w.split(';')[1].split(':')[1]
                        nnum_w      = clsnum_w.split(';')[0].split(':')[1] if clsnum_w.split(';')[0].split(':')[0] == 'Healthy Control' else clsnum_w.split(';')[1].split(':')[1]
                    else:
                        pnum_w      = clsnum_w.split(';')[0].split(':')[1]
                        nnum_w      = clsnum_w.split(';')[1].split(':')[1]
                    p_n_w       = '%s/%s' % (pnum_w,nnum_w)
                else:
                    p_n_w       = None
                if best.SVMstat != None:
                    AUC         = best.SVMaucs.split(';')[0].split('@')[1]
                    for k in best.SVMstat.split(';'):
                        if k.split('@')[0] != 'Healthy Control':
                            TP      = k.split('@')[1].split(':')[0]
                            FP      = k.split('@')[1].split(':')[1]
                            FN      = k.split('@')[1].split(':')[2]
                            TN      = k.split('@')[1].split(':')[3]
                            Sens    = k.split('@')[1].split(':')[4]
                            Spec    = k.split('@')[1].split(':')[5]
                            PPV     = k.split('@')[1].split(':')[6]
                            NPV     = k.split('@')[1].split(':')[7]
                            F1      = k.split('@')[1].split(':')[8]
                            break                        
                else:
                    TP = FP = FN = TN = Sens = Spec = PPV = NPV = F1 = AUC = 'NONE'
                SVMbestFeatureLength = len(best.SVMbestatts)
                best_list   = [Positive     ,Negative   ,p_n    ,p_n_w  ,best.main      ,best.wght      ,best.meas  ,str(best.atpm.num) ,best.SVMlearner.name   ,                                   
                               best.SVMdrtn ,str(SVMbestFeatureLength)  ,'%.2f' % (best.SVMbestacc*100) ,'(%s,%s)' % best.SVMbestparam  ,AUC    ,TP     ,FP     ,
                               FN     ,TN   ,Sens       ,Spec   ,PPV    ,NPV    ,F1     ]
                for k in range(len(best_list)):                        
                    self.sheet[i].write(k+1,cur_col,best_list[k])
        if len(self.dset) != 0:
            self.book.save(self.output_file)
            
            